<template>
  <div class="login-page">
    <el-card class="el-card">
      <!-- header插槽 -->
      <template #header>
        后台管理系统
        <router-link class="link" to="/register">去注册</router-link>
        </template>
      <!-- 表单 -->
      <el-form autocomplete="off" :model="form" :rules="rules" ref="myForm">
        <el-form-item label="用户名：" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
        </el-form-item>

        <el-form-item label="密码：" prop="password">
          <el-input type="password" v-model="form.password" placeholder="请输入密码"></el-input>
        </el-form-item>

        <el-form-item class="tc">
          <el-button type="primary" @click="login" class="login">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <div class="logo"></div>
  </div>
</template>

<script>
import { login } from '@/api/user'
export default {
  name: "LoginIndex",
  data () {
    return {
        form: {
            username: '',
            password: ''
        },
        rules: {
            // 设置对应字段的校验规则
            username: [
                { required: true, message: '请输入用户名', trigger: ['blur'] },
                { min: 5, max: 11, message: '长度 5 到 11 个字符', trigger: ['blur'] }
            ],
            password: [
                { required: true, message: '请输入密码', trigger: ['blur'] },
                { pattern: /^\w{5,11}$/, message: '长度 5 到 10 位的密码', trigger: ['blur'] }
            ]
        }
    }
  },
  methods: {
   async login () {
        try {
            const valid = await this.$refs.myForm.validate()
            if (valid) {
                const res = await login(this.form)
                console.log(res)
                // 判断是否登陆成功
               if (res.code === 0) {
                this.$store.commit('user/setUserToken',res.token)
                this.$message.success('登录成功')
                this.$router.push('/')
               } else {
                this.$message.error('账号或密码错误,请检查!')
               }
            }
        } catch (e) {
            console.log(e)
        }
   },
   resut () {
        this.$refs.myForm.resetFields()
   }
  }
};
</script>

<style lang="less" scoped>
.login-page {
  position: relative;
  min-height: 100vh;
  background: url(https://lanhu.oss-cn-beijing.aliyuncs.com/SketchPnge466a35a7e55683e4f024770ac7a3c8f6e08659a88a7b402d5c5b874853aa1c2) no-repeat center / cover;
  display: flex;
  align-items: center;
  justify-content: space-around;
  .el-card {
    width: 420px;
    margin: 0 auto;

    // 深度作用选择器 scss  ::v-deep   less /deep/
    ::v-deep .el-card__header {
      height: 80px;
      text-align: center;
      font-size: 25px;
    }
  }
   .el-form {
    padding: 0 20px;
  }
  
  .tc {
    text-align: center;
  }

  .login {
    width: 100%;
    background-color: #4F81FF;
  }

  .link {
    display: block;
    margin-top: 5px;
    color: #4F81FF;
    font-size: 12px;
    text-decoration: none;
  }

  .logo {
    position: absolute;
    top: 30px;
    left: 50px;
    width: 100px;
    height: 40px;
    background: url(https://lanhu.oss-cn-beijing.aliyuncs.com/SketchPngd49eceef977cbc6df84eac9150a770d8e5f2326e08feb8d1bd5055a9fa38dc02) no-repeat center / cover;
  }
}
</style>